package com.example.yunifang.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.R.bool;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunifang.R;
import com.example.yunifang.XiangQingActivity;
import com.example.yunifang.bean.GouWuCheBean;
import com.example.yunifang.dao.GouWuCheDao;
import com.example.yunifang.fragment.Fragment3;
import com.nostra13.universalimageloader.core.ImageLoader;

public class Frgament3Adapter extends BaseAdapter {
	ArrayList<GouWuCheBean> data;
	Context context;
	LinkedList<Boolean> list = new LinkedList<Boolean>();
	LinkedList<Integer> list_num = new LinkedList<Integer>();
	public static double sum;
	public CheckBox box;
	public static LinearLayout fragment3_list_item_bianji;
	boolean myflag;
	boolean myqingling;

	public Frgament3Adapter(ArrayList<GouWuCheBean> data, Context context) {
		super();
		this.data = data;
		this.context = context;
		for (int i = 0; i < data.size(); i++) {
			getSelect().add(false);
		}
		for (int i = 0; i < data.size(); i++) {
			list_num.add(data.get(i).getNum());
		}
	}

	public List<Boolean> getSelect() {
		return list;
	}

	public LinkedList<Integer> getLink() {
		return list_num;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {

		if (view == null) {
			view = View.inflate(context, R.layout.fragment3_list_item, null);
		}
		ImageView img = (ImageView) view
				.findViewById(R.id.fragment3_list_item_img);
		TextView name = (TextView) view
				.findViewById(R.id.fragment3_list_item_name);
		TextView price = (TextView) view
				.findViewById(R.id.fragment3_list_item_price);
		TextView num = (TextView) view
				.findViewById(R.id.fragment3_list_item_num);
		Button fragment3_list_item_jian = (Button) view
				.findViewById(R.id.fragment3_list_item_jian);
		Button fragment3_list_item_jia = (Button) view
				.findViewById(R.id.fragment3_list_item_jia);
		final TextView fragment3_list_item_btn_num = (TextView) view
				.findViewById(R.id.fragment3_list_item_btn_num);
		fragment3_list_item_btn_num.setText(list_num.get(position) + "");
		fragment3_list_item_bianji = (LinearLayout) view
				.findViewById(R.id.fragment3_list_item_bianji);
		fragment3_list_item_jia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				list_num.set(position, (list_num.get(position) + 1));
				fragment3_list_item_btn_num.setText(list_num.get(position) + "");
				notifyDataSetChanged();
			}
		});
		fragment3_list_item_jian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// i = data.get(position).getNum();
				if (list_num.get(position) < 1) {
					Toast.makeText(context, "至少选择一件商品", 0).show();
				} else {
					list_num.set(position, (list_num.get(position) - 1));

					fragment3_list_item_btn_num.setText(list_num.get(position)
							+ "");
				}
				notifyDataSetChanged();
			}
		});
		ImageLoader.getInstance()
				.displayImage(data.get(position).getImg(), img);
		name.setText(data.get(position).getName());
		price.setText("￥" + data.get(position).getPrice());
		num.setText("数量：" + data.get(position).getNum());
		box = (CheckBox) view.findViewById(R.id.fragment3_list_item_box);
		box.setChecked(list.get(position));

		DecimalFormat format = new DecimalFormat("#0.00");
		String price_num = format.format(sum);
		Fragment3.fragment3_zongji.setText("总计:￥" + price_num);
		box.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				list.set(position, !list.get(position));
				if (list.contains(false)) {
					Fragment3.fragment3_box.setChecked(false);
					Fragment3.sum = 0;
				} else {
					Fragment3.fragment3_box.setChecked(true);
					Fragment3.sum = sum;
				}
				if (list.get(position) == true) {
					sum = sum
							+ Float.valueOf(data.get(position).getNum()
									* data.get(position).getPrice() + "");
				} else if (list.get(position) == false) {
					sum = sum
							- Float.valueOf(data.get(position).getNum()
									* data.get(position).getPrice() + "");
				}
				//System.out.println(sum+"*********");
				notifyDataSetChanged();
				DecimalFormat format = new DecimalFormat("#0.00");
				String price_num = format.format(sum);
				Fragment3.fragment3_zongji.setText("总计:￥" + price_num);

			}
		});
		if(myflag==true){
			sum=0.00;
		}
		
		
		if (myflag == true) {
			fragment3_list_item_bianji.setVisibility(View.VISIBLE);
			num.setVisibility(View.INVISIBLE);
			notifyDataSetChanged();
		} else {
			fragment3_list_item_bianji.setVisibility(View.GONE);
			num.setVisibility(View.VISIBLE);
			notifyDataSetChanged();
		}
		return view;
	}

	public void showBtn(boolean flag) {
		myflag = flag;
		notifyDataSetChanged();
	}

}
